 package cn.com.ccxe.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ccxe.core.entity.Favorite;
import cn.com.ccxe.core.entity.Keyword;
import cn.com.ccxe.dao.OperationStatus;
import cn.com.ccxe.service.FavoriteBussiness;
import cn.com.ccxe.web.form.FavoriteActionForm;

import com.ccxe.manage.service.CurrUserInfo;

public class FavoriteAction extends BasciAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3071478624457712899L;
	private static final Logger logger = LoggerFactory.getLogger(FavoriteAction.class);
	@Override
	protected void dispatch(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		FavoriteActionForm actionForm = new FavoriteActionForm(req);
		//请求的方法
		String method = actionForm.getMethod();
		if(isEmpty(method)) {
			method = method.trim();
		}
		CurrUserInfo userInfo = actionForm.getUserInfo();
		if(userInfo == null) {
			logger.info("用户信息已过期，请重新登录！");
			resp.sendRedirect("/login.jsp");
			invalidateSession(req);
			return;
		}
		if(!isEmpty(method)) {
			show(req, resp, actionForm);
			return;
		}
		if("show".equals(method)) {
			show(req, resp, actionForm);
			return;
		}
		if("addFavorite".equals(method)) {
			logger.info("方法未实现....");
			return;
		}
		if("toAddFavorite".equals(method)) {
			toAddFavorite(req, resp, actionForm);
			return;
		}
		if("update".equals(method)) {
			update(req, resp, actionForm);
			return;
		}
		if("del".equals(method)) {
			del(req, resp, actionForm);
			return;
		}
		if("delKeyword".equals(method)) {
			delKeyword(req, resp, actionForm);
			return;
		}
	}
	
	private boolean isEmpty(String str) {
		return !StringUtils.isEmpty(str);
	}

	private void show(HttpServletRequest req,
				 HttpServletResponse resp, 
				 FavoriteActionForm actionForm)
			throws ServletException,
				   IOException {
		FavoriteBussiness favoriteBussiness = new FavoriteBussiness();
		List<Favorite> favoriteList = favoriteBussiness.getAllByUser(actionForm.getUserInfo().getUserId());
		req.setAttribute("favoriteList",favoriteList);
		forward(req, resp, "/zebra/customer/favorite/show.jsp");
	}
	
	/**
	* 根据类型跳转到关键字选择页面。
	* @param req
	* @param resp
	* @param actionForm
	* @throws ServletException
	* @throws IOException
	*/
	private void toAddFavorite(HttpServletRequest req,
			   HttpServletResponse resp, 
			   FavoriteActionForm actionForm)
			throws ServletException,
			   	   IOException {
		FavoriteBussiness favoriteBussiness = new FavoriteBussiness();
		List<Keyword> keywordListIsNot = favoriteBussiness.getKeywordListIsNot(
							actionForm.getMonitorTypeId(),
							actionForm.getUserInfo().getUserId(),
							actionForm.getWebsiteId());
		
		List<Keyword> keywordList = favoriteBussiness.getKeywordList(
				actionForm.getMonitorTypeId(),
				actionForm.getUserInfo().getUserId(),
				actionForm.getWebsiteId());
		
		req.setAttribute("keywordList", keywordList);
		req.setAttribute("keywordListIsNot", keywordListIsNot);
		req.setAttribute("args", actionForm);
		forward(req, resp, "/zebra/customer/favorite/keywordList.jsp");
	}
	
	/**
	* 修改类型名称
	* @param req
	* @param resp
	* @param actionForm
	* @throws ServletException
	* @throws IOException
	*/
	private void update(HttpServletRequest req, 
				  HttpServletResponse resp, 
				  FavoriteActionForm actionForm)
			throws ServletException, 
			   	   IOException {
	}

	/**
	* 删除操作
	* <p>在进行删除操作时，必须确保任何站点、关键字属于该类别。</p>
	* @param req
	* @param resp
	* @param actionForm 
	* @throws ServletException
	* @throws IOException
	*/
	private void del(HttpServletRequest req, 
					 HttpServletResponse resp, 
					 FavoriteActionForm actionForm)
			throws ServletException,
			   	   IOException {
		FavoriteBussiness fwb = new FavoriteBussiness();
		OperationStatus status = fwb.del(actionForm.getId());
		
		switch (status) {
		case SUCCESS:
//			forward(req, resp,"/zebra/customer/search/");
			susscess(req, resp, "删除成功！");
			break;
		default:
			error(req, resp, "删除操作失败，请确认操作步骤是否正确！");
			break;
		}
	}
	private void delKeyword(HttpServletRequest req, 
				 HttpServletResponse resp, 
				 FavoriteActionForm actionForm)
			throws ServletException,
				   IOException {
		FavoriteBussiness fwb = new FavoriteBussiness();
		OperationStatus status = fwb.delKeyword(
				actionForm.getId(),
				actionForm.getKeywordId());
		
		switch (status) {
		case SUCCESS:
			redirect(req, resp,
					"/zebra/customer/favorite/FavoriteAction?"
					+"method=toAddFavorite&websiteId="+actionForm.getWebsiteId()
					+"&monitorTypeId="+actionForm.getMonitorTypeId()
					);
			break;
		default:
			error(req, resp, "删除操作失败，请确认操作步骤是否正确！");
			break;
		}
	}
}
